import { create, update, remove, query } from '../../../../../framework/service/index';
import { ToastTips } from '../../../../../common/toastTips/tosat';
import { routerRedux } from 'dva/router';

export default {

  namespace: 'jobIntention',

  state: {
    data: [],
    selectJobType: [],
    selectworkingArea: [],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/job-intention') {
          dispatch({
            type: 'query',
          })
        }
      })
    },
  },



  effects: {

    *query({ payload }, { call, put }) {
      const { code, data, message } = yield call(query, '/api/applicant/intention', {});
      if(code == 200){
        // console.log('data',JSON.stringify(data));
        yield put({
          type: 'querySuccess',
          payload: {
            data: data.data
          }
        })
      }

    },

    *onSelect({payload},{call,put}){

      console.log('payload.data',payload.data);

      if (payload.select == 'selectJobType') {
        yield put({
          type:'querySuccess',
          payload:{
            selectJobType:payload.data ? payload.data : []
          }
        })
      } else {
        yield put({
          type:'querySuccess',
          payload:{
            selectworkingArea:payload.data ? payload.data : []
          }
        })
      }

    },

    *onSublime({payload},{call,put,select}){
      const selectType =  yield select(({jobIntention})=>jobIntention.selectJobType)
      const selectArea =  yield select(({jobIntention})=>jobIntention.selectworkingArea)
      let jobTypeIds = [];
      let areasIds = [];
      if(selectType && selectType.length > 0){
        selectType.map((item, index) => {
          jobTypeIds.push(item.id);
        })
      }
      if(selectArea && selectArea.length > 0){
        selectArea.map((item, index) => {
          areasIds.push(item.id);
        })
      }

      const postData = {
        jobPlanTypes:jobTypeIds,
        areas:areasIds
      }

      const { code, data, message } = yield call(create, '/api/applicant/intention', {...postData});
      if(code == 200){
        // console.log('data',JSON.stringify(data));
        ToastTips('保存成功', 2000, null);

        yield put(routerRedux.goBack());
      }else{
        ToastTips('保存失败', 2000, null);
        console.log('保存失败', message);
      }
    }
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
